package com.jonniespratley.codegen.utils
{
	import com.adobe.net.URI;
	import com.jonniespratley.codegen.view.forms.ServiceNameValueForm;
	import com.jonniespratley.codegen.view.forms.ServiceNameValueInput;

	import flash.utils.ByteArray;

	import mx.core.UIComponent;

	public class CodeGenUtilities
	{
		public function CodeGenUtilities(){}

		static public function getRealURL( path:String ):String
		{
			var pattern:RegExp = /^(..\/)/gi;
			var str:String = path.replace( pattern, '' );

			return str;
		}

		static public function cleanText( str:String ):String
		{
			var s:String = str.split('\\n').join('\n').split('\\"').join('"').split("\\'").join("'");

			return s;
		}

		static public function getUTF8Bytes( s:String ):Number
		{
			var byteArray:ByteArray = new ByteArray();
				byteArray.writeUTFBytes( s );

			return byteArray.length;
		}

		static public function setConsoleEndpoint( url:String ):String
		{
			if ( url.indexOf( 'http' ) != -1 )
			{
				if ( url.indexOf( "browser" ) < url.length - 1 - 7 )
				{
					url = url.substr( 0, url.lastIndexOf( '/' ) );
				}

				url = url.substr( 0, url.lastIndexOf( '/' ) );
				url += "/library/FlexService.php";

			} else {
				url = "http://localhost/CodeGen/library/FlexService.php";
			}

			return url;

			trace( url );
		}

		static public function formatBytes( num:Number ):String
		{
			if( num < 1024 )
			{
				return Math.round(num) + " bytes";
			}
			else if(num < 1024*1024)
			{
				return Math.round(num/1024*10)/10 + " KB";
			}
			else
			{
				return Math.round(num/1024/1024*10)/10 + " MB";
			}
		}


		static public function buildURL( input:String, params:Array ):String
		{
			var output:String = '';

			var uri:URI = new URI( input );

			//loop the params

			for( var i:int = 0; i < params.length; i++)
			{
				for( var s:String in params[ i ] )
				{
					uri.setQueryValue( s, params[ i ][ s ] );
				}
			}
			output = uri.toString();

			return output;
		}

		//Get the n/v from the inputs
		static public function setParameters( nvForm:UIComponent ):Array
		{
			var serviceNVForm:ServiceNameValueForm = ServiceNameValueForm( nvForm );

			var paramArray:Array = [];

			var numChild:int = serviceNVForm.getNameValuePairChildren();

			for ( var i:int = 0; i < numChild; i++ )
			{
				var nv:ServiceNameValueInput = ServiceNameValueInput( serviceNVForm.getChildAt( i ) );
				var obj:Object = new Object();
					obj[ nv.txt_queryName.text ] = nv.txt_queryValue.text;

				paramArray.push( obj );
			}
			return paramArray;
		}

		static public function createObjectFromURLEncoded( url:String ):Object
		{
			//url ( http://localhost/CodeGen/library/ClassInspector.php?url=test&mode=GetData&user=Flex&this=tight  )
			var query:String = url.split ( "?" )[ 1 ];//Pulls out this( url=test&mode=GetData&user=Flex&this=tight )

			var temp:Array;// [0] = url, [1] = test

			var args:Array = query.split ( "&" );// [0] mode=GetData, [1] user=Flex, [2] this=tight

			var decoded:Object = {};

			//Loop all the arguments array
			while ( args.length )
			{
				//Temp array = remove one at a time after you split where the = sign is
				temp = args.shift().split ( "=" );

				//new decoded is name = value
				//or name = NAME
				//value = VALUE
				decoded[ temp[ 0 ] ] = temp[ 1 ];
				decoded[ 'name' ] = temp[ 0 ];
				decoded[ 'value' ] = temp[ 1 ];
			}
			return decoded;
		}


		static public function createArrayFromURLEncoded( url:String ):Array
		{
			//url ( http://localhost/CodeGen/library/ClassInspector.php?url=test&mode=GetData&user=Flex&this=tight  )
			var query:String = url.split ( "?" )[ 1 ];//Pulls out this( url=test&mode=GetData&user=Flex&this=tight )

			var temp:Array;// [0] = url, [1] = test

			var args:Array = query.split ( "&" );// [0] mode=GetData, [1] user=Flex, [2] this=tight

			var decoded:Array = [];

			while ( args.length )
			{
				temp = args.shift().split ( "=" );

				var o:Object = {};
					o[temp[ 0 ]] = temp[ 1 ];


					decoded.push( o );
			}


			return decoded;
		}


	}
}